package com.alliance.shuwei.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alliance.shuwei.entity.User;
import com.alliance.shuwei.service.UserService;

@Controller
@RequestMapping("/admin/users")
public class UserController {
	@Autowired
	private UserService userService;


	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	public User addUser(@Valid User user) {
		return userService.createUser(user);
	}

	@RequestMapping(value = "{id}", params = "isActive", method = RequestMethod.POST)
	public void updateUserStatus(@PathVariable("id") Integer id, @RequestParam String active) {
		User user = userService.getUserById(id);
		if ("Active".equalsIgnoreCase(active)) {
			user.setActive(true);
		} else {
			user.setActive(false);
		}
		userService.updateUser(user);
	}

	@RequestMapping(method = RequestMethod.GET)
	@ResponseBody
	public List<User> getAllUsers() {
		return userService.getAllUsers();
	}
}
